"""
数据库连接测试工具
用于验证数据库连接和数据访问
"""
from app import app, db
from app.models import Employee, Organization

def test_db_connection():
    """测试数据库连接和数据访问"""
    with app.app_context():
        try:
            # 测试组织数据访问
            org = Organization.query.first()
            if org:
                print("组织数据访问成功:")
                print(f"组织名称: {org.name}")
                print(f"总人数: {org.total_employees}")
                print(f"正式工人数: {org.formal_employees}")
                print(f"平均年龄: {org.avg_age}")
                print(f"平均司龄: {org.avg_tenure}")
            else:
                print("未找到组织数据")

            # 测试员工数据访问
            employees = Employee.query.all()
            if employees:
                print("\n员工数据访问成功:")
                for emp in employees:
                    print(f"姓名: {emp.name}, 部门: {emp.department}, 职位: {emp.position}")
            else:
                print("未找到员工数据")

            print("\n数据库连接测试完成")
            return True

        except Exception as e:
            print(f"数据库访问出错: {str(e)}")
            return False

if __name__ == "__main__":
    test_db_connection()
